package com.project.controller;

import com.project.model.entity.SubSystem;
import com.project.result.Result;
import com.project.service.SubSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/subSystem")
public class SubSystemController {
    private final SubSystemService subSystemService;

    @Autowired
    public SubSystemController(SubSystemService subSystemService) {
        this.subSystemService = subSystemService;
    }
    @PostMapping("/saveOrUpdate")
    public Result<SubSystem> saveOrUpdateSubSystem(@RequestBody SubSystem subSystem) {
        try {
            if (subSystem == null) {
                return Result.fail("子系统内容为空");
            }

            if (subSystemService.getSubSystemById(subSystem.getId()) == null) {
                SubSystem savedSubSystem = subSystemService.addSubSystem(subSystem);
                return savedSubSystem != null ? Result.ok(savedSubSystem) : Result.fail("保存失败");
            } else {
                SubSystem updatedSubSystem = subSystemService.updateSubSystem(subSystem);
                return updatedSubSystem != null ? Result.ok(updatedSubSystem) : Result.fail("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }


    @DeleteMapping("/delete")
    public Result<Void> deleteSubSystem(@RequestParam Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.fail("ID值无效");
            }
            subSystemService.deleteSubSystem(id);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @GetMapping("/getById")
    public Result<SubSystem> getSubSystemById(@RequestParam Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.fail("ID值无效");
            }
            SubSystem subSystem = subSystemService.getSubSystemById(id);
            return subSystem != null ? Result.ok(subSystem) : Result.fail("未找到该子系统");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @GetMapping("/getAll")
    public Result<List<SubSystem>> getAllSubSystems() {
        try {
            List<SubSystem> subSystems = subSystemService.getAllSubSystems();
            return !subSystems.isEmpty() ? Result.ok(subSystems) : Result.fail("没有数据");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @GetMapping("/getByReportId")
    public Result<List<SubSystem>> getSubSystemsByReportId(@RequestParam Integer reportId) {
        System.out.println("report前"+reportId);
        try {
            System.out.println("report"+reportId);
            List<SubSystem> subSystems = subSystemService.getSubSystemsByReportId(reportId);
            return !subSystems.isEmpty() ? Result.ok(subSystems) : Result.fail("没有数据");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }


}

